/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pdasim;

import com.mxgraph.swing.mxGraphComponent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collections;
import java.util.Stack;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Camilo
 */
public class PDASIM extends JFrame {

    PDA pda;
    JTextField input;
    JButton nextstep;
    String inputline;
    String filedir;

    public PDASIM() {

        JPanel contentPane = new JPanel(new BorderLayout());
        JList pila = new JList();
        JList log = new JList();
        JPanel menutop = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel menubot = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton loadfile = new JButton("Load File");
        JButton reload = new JButton("Reload");
        nextstep = new JButton("Step");
        input = new JTextField();
        input.setEnabled(false);
        nextstep.setEnabled(false);
        filedir = null;

        Font font = new Font(Font.MONOSPACED, Font.LAYOUT_RIGHT_TO_LEFT, 30);
        Font fontinput = new Font(Font.MONOSPACED, Font.CENTER_BASELINE, 20);
        input.setFont(fontinput);
        pila.setFont(font);

        pila.setFixedCellHeight(50);
        pila.setFixedCellWidth(200);
        log.setFixedCellHeight(20);
        log.setFixedCellWidth(200);

        Dimension dim = new Dimension(this.getWidth(), 40);
        menutop.setPreferredSize(dim);
        menubot.setPreferredSize(dim);




        Dimension inputsize = new Dimension(600, 30);
        input.setPreferredSize(inputsize);

        ActionListener actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                JFileChooser fc = new JFileChooser();
                fc.showOpenDialog(fc);
                File f = fc.getSelectedFile();
                if (f != null) {
                    filedir = f.getAbsolutePath();
                    setgraph(filedir);
                }
                input.setEnabled(true);
                nextstep.setEnabled(true);
                inputline = "";
            }
        };
        
        ActionListener reaload_Listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(filedir!=null){
                    setgraph(filedir);
                    inputline="";
                    input.setEnabled(true);
                nextstep.setEnabled(true);
                }
            }
        };

        ActionListener step = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (inputline.equals("")) {
                    inputline = input.getText();
                }
                inputline = getnextstep(inputline);
                System.out.println(inputline);
                input.setEnabled(false);
                if(!pda.isStatus_()){
                    input.setEnabled(false);
                    nextstep.setEnabled(false);
                }

            }
        };

        loadfile.addActionListener(actionListener);
        nextstep.addActionListener(step);
        reload.addActionListener(reaload_Listener);


        menutop.add(loadfile);
        menubot.add(input);
        menubot.add(nextstep);
        menubot.add(reload);

        menutop.setBorder(BorderFactory.createLineBorder(Color.black));
        menubot.setBorder(BorderFactory.createLineBorder(Color.black));
        pila.setBorder(BorderFactory.createLineBorder(Color.black));
        log.setBorder(BorderFactory.createLineBorder(Color.black));



        JScrollPane scrollPila = new JScrollPane(pila);
        JScrollPane scrollLog = new JScrollPane(log);

        contentPane.add(scrollPila, BorderLayout.EAST);
        contentPane.add(scrollLog, BorderLayout.WEST);
        contentPane.add(menutop, BorderLayout.PAGE_START);
        contentPane.add(menubot, BorderLayout.PAGE_END);


        this.setContentPane(contentPane);


    }

    public String getnextstep(String inputline) {
        String nextline = inputline;
        nextline = pda.pdastep(nextline);
        mxGraphComponent graphComponent = pda.paint();
        graphComponent.setBorder(BorderFactory.createLineBorder(Color.black));
        this.getContentPane().remove(4);
        this.getContentPane().add(graphComponent, 4);
        this.getContentPane().remove(0);
        JList pila = new JList();
        Font font = new Font(Font.MONOSPACED, Font.LAYOUT_RIGHT_TO_LEFT, 30);
        pila.setFont(font);
        pila.setBorder(BorderFactory.createLineBorder(Color.black));
        pila.setEnabled(false);
        
        pila.setFixedCellHeight(50);
        pila.setFixedCellWidth(200);
        Stack<String> stack1 = new Stack<String>();
        stack1.addAll(pda.getStack());
        pila.setListData(stack1);
        Collections.reverse(stack1);
        int cantselected = pda.getCantpush();
        pila.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        pila.setSelectionInterval(0, cantselected-1);
        
        pila.setSelectionBackground(Color.red);
        JScrollPane scrollPila = new JScrollPane(pila);
        this.getContentPane().add(scrollPila, BorderLayout.EAST, 0);

        this.getContentPane().remove(1);
        JList log = new JList();
        log.setBorder(BorderFactory.createLineBorder(Color.black));

        log.setFixedCellHeight(20);
        log.setFixedCellWidth(200);
        Stack<String> log1 = new Stack<String>();
        log1.addAll(pda.getLog());
        log.setListData(log1);
        JScrollPane scrollPila1 = new JScrollPane(log);
        this.getContentPane().add(scrollPila1, BorderLayout.WEST, 1);
        this.validate();
        this.repaint();
        return nextline;

    }

    public void setgraph(String dir) {
        if (dir != null || !dir.equals("")) {
            this.getContentPane().remove(0);
            JList pila = new JList();
            Font font = new Font(Font.MONOSPACED, Font.LAYOUT_RIGHT_TO_LEFT, 30);
            pila.setFont(font);
            pila.setBorder(BorderFactory.createLineBorder(Color.black));
            pila.setFixedCellHeight(50);
            pila.setFixedCellWidth(200);
            String[] data = {"Z"};
            pila.setListData(data);
            pila.setEnabled(false);
            JScrollPane scrollPila = new JScrollPane(pila);
            this.getContentPane().add(scrollPila, BorderLayout.EAST, 0);

            this.getContentPane().remove(1);
            JList log = new JList();
            log.setBorder(BorderFactory.createLineBorder(Color.black));
            log.setFixedCellHeight(20);
            log.setFixedCellWidth(200);
            JScrollPane scrollPila1 = new JScrollPane(log);
            this.getContentPane().add(scrollPila1, BorderLayout.WEST, 1);


            pda = new PDA(dir);
            pda.generar();
            mxGraphComponent graphComponent = pda.paint();
            graphComponent.setName("graph");
            graphComponent.setBorder(BorderFactory.createLineBorder(Color.black));

            if (this.getContentPane().getComponentCount() > 4) {
                this.getContentPane().remove(4);
                this.getContentPane().add(graphComponent, 4);

            } else {
                this.getContentPane().add(graphComponent);

            }
            this.validate();
            this.repaint();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PDASIM f = new PDASIM();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dimension = tk.getScreenSize();
        int screenWidth = dimension.width;
        int screenHeight = dimension.height;
        f.setSize(screenWidth, screenHeight);

        f.setVisible(true);
    }
}
